package handle

import (
	"fmt"
	"log"
	"net/http"
	"text/template"

	"github.com/unrolled/render"
)

func LoginHandler(w http.ResponseWriter, r *http.Request) {
	println(r.Method)
	if r.Method == "GET" {
		t, _ := template.ParseFiles("d:/goworks/src/hw07/assets/login.html")
		log.Println(t.Execute(w, nil))
	} else {
		r.ParseForm()
		fmt.Println(r.Form)
		fmt.Println("username:", r.Form["username"])
		fmt.Println("password:", r.Form["password"])

		formatter := render.New(render.Options{
			Directory:  "d:/goworks/src/hw07/templates",
			Extensions: []string{".html"},
			IndentJSON: true,
		})

		formatter.HTML(w, http.StatusOK, "sheet", struct {
			Un string `json:"username"`
			Pw string `json:"password"`
		}{Un: r.Form["username"][0], Pw: r.Form["password"][0]})
	}
}
